﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.Model;
using ProdeManagmente.Libs;
using ProdeManagmente.DAO;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmResult : Form
    {
        private Result result;
        private ResultService resultService;
        private bool changed = false;

        public FrmResult(Result result)
        {
            InitializeComponent();
            this.result = result;
            resultService = ResultService.getResultServiceInstance();
            loadData();
            changedValue(false);
        }

        private void loadData()
        {
            Match match = result.getMatch();
            txtTournament.Text = match.getTournament().getName();
            dtpDate.Value = match.getDate();
            txtTeam1.Text = match.getTeam1().getName();
            txtTeam2.Text = match.getTeam2().getName();
            chkFinal.Checked = result.getFinalResult();

            txtValTeam1.Text = result.getVTeam1() + "";
            txtValTeam2.Text = result.getVTeam2() + "";

            txtPenTeam1.Text = result.getPTeam1() + "";
            txtPenTeam2.Text = result.getPTeam2() + "";

            if (result.getVTeam1() < 0)
                txtValTeam1.Text = "";
            if (result.getVTeam2() < 0)
                txtValTeam2.Text = "";
            if (result.getPTeam1() < 0)
                txtPenTeam1.Text = "";
            if (result.getPTeam2() < 0)
                txtPenTeam2.Text = "";

            if (result.getFinalResult())
            {
                txtValTeam1.Enabled = false;
                txtValTeam2.Enabled = false;
                txtPenTeam1.Enabled = false;
                txtPenTeam2.Enabled = false;
                chkFinal.Enabled = false;
            }
        }

        private void blindData()
        {
            try
            {
                result.setFinalResult(chkFinal.Checked);

                if (txtValTeam1.Text == "")
                    result.setVTeam1(-1);
                else
                    result.setVTeam1(Convert.ToInt32(txtValTeam1.Text));

                if (txtValTeam2.Text == "")
                    result.setVTeam2(-1);
                else
                    result.setVTeam2(Convert.ToInt32(txtValTeam2.Text));

                if (txtPenTeam1.Text == "")
                    result.setPTeam1(-1);
                else
                    result.setPTeam1(Convert.ToInt32(txtPenTeam1.Text));

                if (txtPenTeam2.Text == "")
                    result.setPTeam2(-1);
                else
                    result.setPTeam2(Convert.ToInt32(txtPenTeam2.Text));
            }
            catch (Exception error)
            {
                throw new BusinessException("401", "Error en los datos ingresados");
            }
        }

        private void save()
        {
            try
            {
                blindData();
                result = resultService.save(result);
                changedValue(false);
                loadData();
                lblInfo.Text = "Operacion exitosa";
            }
            catch (BusinessException error)
            {
                new MensajeSalida(error).showMensaje();
            }
        }

        private bool exit()
        {
            if (changed)
            {
                DialogResult result = MessageBox.Show("Desea grabar los cambios?", "Salir", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                {
                    save();
                    return false;
                }
                else if (result == DialogResult.No)
                {
                    changedValue(false);
                    return false;
                }
                else if (result == DialogResult.Cancel)
                {
                    return true;
                }
            }
            return false;
        }

        private void changeTitle()
        {
            String title = "Equipo";
            if (changed)
            {
                title = title + "*";
            }
            this.Text = title;
        }

        private void changedValue(bool value)
        {
            changed = value;
            changeTitle();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
        }

        private void txtValTeam1_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void txtValTeam2_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void txtPenTeam1_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void txtPenTeam2_TextChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void chkFinal_CheckedChanged(object sender, EventArgs e)
        {
            changedValue(true);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!exit())
                this.Close();
        }

        private void FrmResult_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = exit();
        }
    }
}
